package cn.edu.nchu.controller;

import cn.edu.nchu.entity.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class Test2Controller {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/search/{id}")
    public CommonResult search(@PathVariable("id") Integer id){
        ServiceInstance serviceInstance = discoveryClient.getInstances("test_service1").get(0);
        System.out.println(serviceInstance.getUri());
        return restTemplate.getForObject(serviceInstance.getUri() + "/search/" + id, CommonResult.class);
//        return restTemplate.getForObject("http://localhost:8071/search/" + id, CommonResult.class);
    }
}